﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IBLL;
using MisDLL;
using System.Data;

namespace MisBLL
{
    public class SupplyDAO : MarshalByRefObject, ISupply
    {
        protected DbHelper dbHelper = new DbHelper();

        //根据编码查询供应商信息
        public MisModel.Supply GetById(string id)
        {
            if (string.IsNullOrEmpty(id))
                return null;
            string sql = string.Format("select * from SUPPLY_TAB where SUPP_ID='{0}'", id);
            DataTable dt=  dbHelper.ExecuteDataTable(sql);
            MisModel.Supply entity = null;
            if (dt != null && dt.Rows.Count > 0){
                entity = new MisModel.Supply();
                entity.SuppID = dt.Rows[0]["SUPP_ID"].ToString();
                entity.SuppName = dt.Rows[0]["SUPPLY"].ToString();
                entity.AllName = dt.Rows[0]["ALLNAME"].ToString();
                entity.Addr = dt.Rows[0]["ADDR"].ToString();
                entity.Post = dt.Rows[0]["POST"].ToString();
                entity.Tele = dt.Rows[0]["TELE"].ToString();
                entity.Bei = dt.Rows[0]["BEI"].ToString();
                entity.Liker = dt.Rows[0]["LINKER"].ToString();
                entity.Bank = dt.Rows[0]["BANK"].ToString();
                entity.BankNo = dt.Rows[0]["BANKNO"].ToString();
                entity.TaxNo = dt.Rows[0]["TAXNO"].ToString();
                entity.F1 = dt.Rows[0]["F1"].ToString();
                entity.F2 = dt.Rows[0]["F2"].ToString();
                entity.Faithje = decimal.Parse(dt.Rows[0]["FAITHJE"].ToString());
                entity.DateF = DateTime.Parse(dt.Rows[0]["DATE_F"].ToString());
                entity.Faith = dt.Rows[0]["FAITH"].ToString();
                entity.Debt = decimal.Parse(dt.Rows[0]["DEBT"].ToString());
                entity.WayJs = dt.Rows[0]["WAY_JS"].ToString();
                entity.Oper = dt.Rows[0]["OPER"].ToString();
                entity.DateL = DateTime.Parse(dt.Rows[0]["DATE_L"].ToString());
                entity.SumJeBuy = decimal.Parse(dt.Rows[0]["SUMJEBUY"].ToString());
                entity.Boss = dt.Rows[0]["BOSS"].ToString();
            }
            return entity;
        }

        //根据名称查询供应商信息
        public DataTable GetByName(string name)
        {
            if (string.IsNullOrEmpty(name))
                return null;
            string sql = string.Format("select SUPP_ID,SUPPLY,ALLNAME,ADDR,POST,TELE,BEI,LINKER,BANK,BANKNO,TAXNO,F1,F2,FAITHJE,DATE_F,FAITH,DEBT,WAY_JS,OPER,DATE_L,SUMJEBUY,BOSS,(SUPP_ID + '  '+F1+'  '+F2+'  '+ SUPPLY) as ItemShow  from SUPPLY_TAB where SUPPLY like '%{0}%' or ALLNAME like '%{1}%'", name, name);
            return dbHelper.ExecuteDataTable(sql);
        }

        //查询供应商列表
        public DataTable GetAll()
        {
            string sql = "select SUPP_ID,SUPPLY,ALLNAME,ADDR,POST,TELE,BEI,LINKER,BANK,BANKNO,TAXNO,F1,F2,FAITHJE,DATE_F,FAITH,DEBT,WAY_JS,OPER,DATE_L,SUMJEBUY,BOSS,(SUPP_ID + '  '+F1+'  '+F2+'  '+ SUPPLY) as ItemShow  from SUPPLY_TAB";
            return dbHelper.ExecuteDataTable(sql);
        }

        //添加供应商信息
        public int Add(MisModel.Supply obj)
        {
            if (string.IsNullOrEmpty(obj.F1.ToString()))
            {
                obj.F1 = "Y";
            }
            string insertSql = "insert into SUPPLY_TAB(SUPP_ID,SUPPLY,ALLNAME,ADDR,POST,TELE,BEI,LINKER,BANK,BANKNO,TAXNO,F1,F2,FAITHJE,DATE_F,FAITH,DEBT,WAY_JS,OPER,DATE_L,BOSS) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}','{16}','{17}','{18}','{19}','{20}')";
            string sql = string.Format(insertSql, obj.SuppID, obj.SuppName, obj.AllName, obj.Addr, obj.Post, obj.Tele, obj.Bei, obj.Liker, obj.Bank, obj.BankNo, obj.TaxNo, obj.F1, obj.F2, obj.Faithje, obj.DateF, obj.Faith, obj.Debt, obj.WayJs, obj.Oper, obj.DateL, obj.Boss);
            return dbHelper.ExecuteNonQuery(sql);
        }

        //修改供应商信息
        public int Update(MisModel.Supply obj)
        {
            string updateSql = "update SUPPLY_TAB set SUPPLY='{0}',ALLNAME='{1}',ADDR='{2}',POST='{3}',TELE='{4}',BEI='{5}',LINKER='{6}',BANK='{7}',BANKNO='{8}',TAXNO='{9}',F1='{10}',F2='{11}',FAITHJE='{12}',DATE_F='{13}',FAITH='{14}',DEBT='{15}',WAY_JS='{16}',OPER='{17}',DATE_L='{18}',BOSS='{19}' where SUPP_ID='{20}'";
            string sql = string.Format(updateSql, obj.SuppName, obj.AllName, obj.Addr, obj.Post, obj.Tele, obj.Bei, obj.Liker, obj.Bank, obj.BankNo, obj.TaxNo, obj.F1, obj.F2, obj.Faithje, obj.DateF, obj.Faith, obj.Debt, obj.WayJs, obj.Oper, obj.DateL, obj.Boss,obj.SuppID);
            return dbHelper.ExecuteNonQuery(sql);
        }

        //根据ID删除供应商信息
        public int Delete(string id)
        {
            string sql = string.Format("delete from SUPPLY_TAB where SUPP_ID='{0}'", id);
            return dbHelper.ExecuteNonQuery(sql);
        }
    }
}
